<?php
namespace Wx\Controller;
use Think\Controller;
use Wx\Model\SendMsgModel;

class OldIndexController extends CommonController {
    // 初始化方法
    public function _initialize(){
		parent::_initialize();
//		$shop_spellgroup=D('shop_spellgroup');//拼团表
//		$shop_spellgroup_copy=D('shop_spellgroup_copy');//小区副本
//		$shop_spellgroup_order=D('shop_spellgroup_order');
//
//		// 调用推送拼团结果模型
//        $model = new SendMsgModel();
//        // 调用退款类
//        Vendor('Weixinpay.Weixinpay');  //导入退款类
//        $refund = new \Weixinpay();     //实例化退款类
//
//		//第一：处理拼团产品达到【结束】时间的
//		$overgood=$shop_spellgroup->where("state=1 and endtime<".time())->select();
//		foreach($overgood as $i=>$one){
//			//该产品下架
//			$shop_spellgroup-> where('id='.$one['id'])->setField('state',4);
//			//第二：处理副本【副本拼团标记失败】
//			$copy['orsuccess']=2;//拼团失败
//			$copy['state']=5;//拼团结束
//			$shop_spellgroup_copy-> where('orsuccess=0 and sg_id='.$one['id'])->save($copy);
//
//			$getcopy=$shop_spellgroup_copy-> where('orsuccess=2 and sg_id='.$one['id'])->select();
//			//循环拼团失败的副本
//			foreach($getcopy as $two){
//				//第三：处理失败副本下-已付款-的订单
//				$allorder = $shop_spellgroup_order->where("c_id=".$two['id'])->select();
//                foreach($allorder as $three){
//                    $sg_id = M('shop_spellgroup_copy')->where('id='.$three['c_id'])->getField('sg_id');
//                    $title = M('shop_spellgroup')->where('id='.$sg_id)->getField('title');
//                    if($three['state']==1){//已付款订单，退款并修改状态
//                        if($three['total_fee']!=0.00){//微信支付的，退回到微信
//                            // 生成退款订单号
//                            $refund_sn = 'RE'.date('YmdHis') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
//                            $shop_spellgroup_order->where('id='.$three['id'])->setField('out_refund_no',$refund_sn);
//                            //退款-①查询退款订单、生成退款订单号
//                            $info =  M('shop_spellgroup_order')->where('id = '.$three['id'])->field('total_fee,order_sn,out_refund_no')->find();
//                            $data = $refund->refund($info);     //为true表示退款成功
//                            $price = $info['total_fee'];
//                        }// end if
//                        if($three['card_fee']!=0.00){//卡额支付的，退回到卡额,增加卡额变动记录
//                            M('member')->where('id='.$three['u_id'])->setInc('credit2',$three['card_fee']);
//                            changeCredit($three['u_id'],'拼团失败','credit2','+',$three['card_fee']);
//                            $price = $three['card_fee'];
//                        }// end if
//                        $shop_spellgroup_order->where('id='.$three['id'])->setField('state',3);
//                    }else if($three['state']==0){//未付款的取消订单
//                        $shop_spellgroup_order->where('id='.$three['id'])->setField('state',8);
//                        $price = $three['total_fee'];
//                    }// end if
//                    //发送系统消息
//                    $s_url = '<a href="http://www.xiangtws.com/Wx/MyGrouporder/orderdetail/id/'.$three['id'].'" style="color: #ff4400">'.$title.'</a>';
//                    sendVoice($three['u_id'],'很抱歉，您的拼团订单'.$s_url.'活动结束前未能成团，拼团失败');
//                    // 推送微信消息
//                    $tourl = 'http://www.xiangtws.com/Wx/MyGrouporder/orderdetail/id/'.$three['id'];   //跳转链接
//                    $model->groupmsg($three['u_id'],'很抱歉，您的拼团订单活动结束前未能成团','拼团失败',$title,$price,$tourl);
//
//                }// end foreach
//
//			}//end foreach
//			//循环拼团失败的副本-end
//
//		}//end foreach



	}//end fun


    public function index(){
		$shop_spellgroup=D('shop_spellgroup');//拼团表
		$shop_spellgroup_copy=D('shop_spellgroup_copy');//小区副本
		$category=D('category');
		$member=D('member');
		
		//全部产品 ————————————————————————————
		$page=1;
		if($_GET['page']>1)$page=$_GET['page'];//瀑布流调用
		$shoparr=$shop_spellgroup->page($page.',6')->where("state=1 and type=0")->order("starttime asc")->select(); // 查询第一页数据
		
		//dump(session());die;
		//查询当前用户小区id
		if(session('u_id')>0) {
			
			$getuser=$member->where("id=".session('u_id'))->find();//当前用户信息
			$this->assign('user',$getuser);
			
			//查询本小区门店信息
            if($getuser['c_id']>0)$getstore = M("shop_store")->where("c_id = {$getuser['c_id']}")->find();//获取该社区绑定店铺
			$this->assign('store',$getstore);
			//本社区店拼团
			if($getstore['id']>0){
				$shop_md=$shop_spellgroup->page($page.',6')->where("state=1 and type=1 and s_id=".$getstore['id'])->order("id desc")->select(); //门店产品
				$shoparr=array_merge($shop_md,$shoparr);
			}
			
            $getcommunity = M("shop_community")->where("id = {$getuser['c_id']}")->find();//获取该社区信息
			$this->assign('community',$getcommunity);
			
			foreach($shoparr as $i=>$one){
				if($getuser["c_id"]>0){
					//已经绑定小区
					//用户本小区拼团信息
					$getcopy=$shop_spellgroup_copy->where("sg_id=".$one['id']." and c_id=".$getuser["c_id"])->find();//用户本小区该商品是否开团
					if($getcopy){
						$shoparr[$i]['havecopy']=1;//表示已经开团
						if($one['min_num']- $getcopy['num']>0){
							$shoparr[$i]['neednum']=$one['min_num']- $getcopy['num'];//用户本小区还差几人份成团
							}else{
								$shoparr[$i]['neednum']=0;//拼团成功，还可以继续参团
						}//end if
						
					}else{
							$shoparr[$i]['havecopy']=0;//未开团
							
					}//end if
				}else{
					//未绑定小区
					$shoparr[$i]['havecopy']=0;//未开团
				}//end if
				//单位 
				$getu=$category->where("id=".$one['unit'])->find();//dump($getu);
				$shoparr[$i]['unit']=$getu['title'];
				//产品第一图片
				$img=explode(',',$one['h_imgs']);
				$shoparr[$i]['f_img']=$img[0];//第一张图片作为封面图
				//发布人头像、姓名
				if($one['u_id']>0)$getinfo=$member->where("id=".$one['u_id'])->find();
				$shoparr[$i]['f_realname']=$getinfo['realname'];
				$shoparr[$i]['f_avatar']=$getinfo['avatar'];
				$shoparr[$i]['actual_num']=$shop_spellgroup_copy->where("sg_id=".$one['id'])->sum('num');//已售多少份
				
			}//end foreach
			//全部产品 ————————————————————————————end
			//即将成团（本小区）————————————————————————————————
			if($getuser["c_id"]>0){
				$getneedgood=$shop_spellgroup_copy->where("orsuccess=0 and state=0 and c_id=".$getuser["c_id"])->select();//dump($getneedgood);die;
				foreach($getneedgood as $k=>$one){
					$getgood=$shop_spellgroup->where("id=".$one['sg_id'])->find();
					//首页图、类型、标题、规格、拼团价、sg_id、发布人头像姓名、已售多少份、还差多少成团
					//产品第一图片 
					$img=explode(',',$getgood['h_imgs']);
					$getneedgood[$k]['f_img']=$img[0];//第一张图片作为封面图
					$getneedgood[$k]['type']=$getgood['type'];
					$getneedgood[$k]['title']=$getgood['title'];
					$getneedgood[$k]['u_num']=$getgood['u_num'];
					$getu=$category->where("id=".$getgood['unit'])->find();
					$getneedgood[$k]['unit']=$getu['title'];
					$getneedgood[$k]['actual_price']=$getgood['actual_price'];
					//发布人头像、姓名
					$shop_spellgroup=D('shop_spellgroup');
					$getsg=$shop_spellgroup->where("id=".$one['sg_id'])->find();
					$getinfo=$member->where("id=".$getsg['u_id'])->find();
					$getneedgood[$k]['f_realname']=$getinfo['realname'];
					$getneedgood[$k]['f_avatar']=$getinfo['avatar'];
					
					$getneedgood[$k]['actual_num']=$shop_spellgroup_copy->where("sg_id=".$one['sg_id'])->sum('num');//已售多少份
					$getneedgood[$k]['neednum']=$getgood['min_num']-$one['num'];
				}//end foreach
				$this->assign('needgoodarr',$getneedgood);
			}//end if
			//即将成团（本小区）————————————————————————————————end
		}//end if
		$this->assign('shoparr',$shoparr);
		if($_GET['page']>1)$this->ajaxReturn($shoparr,'json');//瀑布流调用返回
		//新上架产品——————————————————————————————————
		$newshoparr=$shop_spellgroup->page($page.',6')->where("state=1 and (".time().">starttime and ".time()."<endtime)")->order("starttime asc")->select(); // 查询第一页数据
		//dump(session());die;
		//查询当前用户小区id
		if(session('u_id')>0) {
			
			foreach($newshoparr as $i=>$one){
				if($getuser["c_id"]>0){
					//已经绑定小区
					//用户本小区拼团信息
					$getcopy=$shop_spellgroup_copy->where("sg_id=".$one['id']." and c_id=".$getuser["c_id"])->find();//用户本小区该商品是否开团
					if($getcopy){
						$newshoparr[$i]['havecopy']=1;//表示已经开团
						if($one['min_num']- $getcopy['num']>0){
							$newshoparr[$i]['neednum']=$one['min_num']- $getcopy['num'];//用户本小区还差几人份成团
							}else{
								$newshoparr[$i]['neednum']=0;//拼团成功，还可以继续参团
						}//end if
						
					}else{
							$newshoparr[$i]['havecopy']=0;//未开团
							
					}//end if
				}else{
					//未绑定小区
					$newshoparr[$i]['havecopy']=0;//未开团
				}//end if
				//产品第一图片
				$img=explode(',',$one['h_imgs']);
				$newshoparr[$i]['f_img']=$img[0];//第一张图片作为封面图
				//发布人头像、姓名
				if($one['u_id']>0)$getinfo=$member->where("id=".$one['u_id'])->find();
				$newshoparr[$i]['f_realname']=$getinfo['realname'];
				$newshoparr[$i]['f_avatar']=$getinfo['avatar'];
				$newshoparr[$i]['actual_num']=$shop_spellgroup_copy->where("sg_id=".$one['id'])->sum('num');//已售多少份
				
			}//end foreach
		}//endif
		//新上架产品——————————————————————————————————end
		$this->assign('newshoparr',$newshoparr);
		//即将上架————————————————————————————
		$willgood=$shop_spellgroup->page($page.',6')->where("state=1 and starttime>".time())->order("starttime asc")->select(); // 查询第一页数据
		foreach($willgood as $k=>$one){
			$getgood=$shop_spellgroup->where("id=".$one['id'])->find();
			//首页图、类型、标题、规格、拼团价、sg_id、发布人头像姓名
			//产品第一图片 
			$img=explode(',',$getgood['h_imgs']);
			$willgood[$k]['f_img']=$img[0];//第一张图片作为封面图
			$willgood[$k]['type']=$getgood['type'];
			$willgood[$k]['title']=$getgood['title'];
			$willgood[$k]['u_num']=$getgood['u_num'];
			$getu=$category->where("id=".$getgood['unit'])->find();
			$willgood[$k]['unit']=$getu['title'];
			$willgood[$k]['actual_price']=$getgood['actual_price'];
			//发布人头像、姓名
			if($one['u_id']>0)$getinfo=$member->where("id=".$one['u_id'])->find();
			$willgood[$k]['f_realname']=$getinfo['realname'];
			$willgood[$k]['f_avatar']=$getinfo['avatar'];
			//上架时间、关注人数
			$willgood[$k]['starttime']=$getgood['starttime'];
		}//end foreach
		$this->assign('willgood',$willgood);
		//即将上架————————————————————————————end
		//检测该用户是否有成功的拼团或者试吃，且该小区没有门店
		if(session('u_id')>0){
			
			$foretaste_order=D('foretaste_order');//试吃试用订单表
			$shop_spellgroup_order=D('shop_spellgroup_order');//拼团订单表
			
			$havegood=0;//默认无
			$getfo=$foretaste_order->where("state=1 and u_id=".session('u_id'))->find();
			$getso=$shop_spellgroup_order->where("state=4 and u_id=".session('u_id'))->find();
			if($getfo || $getso)$havegood=1;//有成功的拼团或者试吃
			
			$havestore=0;//默认无店铺
			$shop_store=D('shop_store');
			if($getuser['c_id']>0)$getsp=$shop_store->where("c_id =".$getuser['c_id'])->find();
			if($getsp)$havestore=1;//有店铺
			
			//检测是否推荐过店铺
			$rec_s=0;//未推荐过
			$shop_recommend=D('shop_recommend');
			$getsr=$shop_recommend->where("u_id=".session('u_id'))->find();
			if($getsr)$rec_s=1;//推荐过店铺
			
			if($havegood==1 && $havestore==0 && session('recommend')!=1 && $rec_s==0){
				$this->assign('recommend',0);//弹出推荐店铺
				session('recommend',1);//弹过一次
			}else{
				$this->assign('recommend',1);//不弹出推荐店铺
			}
		}
		//检测该用户是否有成功的拼团或者试吃，且该小区没有门店-end 
		
        $this->display('group');
    }//end fun


	public function logout(){
		session(null); // 清空当前的session
		echo "退出";
	}//end fun

    public function binding(){
        $this->display();
    }
}